<template>
  <div>
    <div style="width: 100px; margin: 0px auto 10px">
      <el-button @click="dialogVisible = true" type="primary" plain>添加公告</el-button>
    </div>
    <el-table border :data="tableData" style="width: 100%; margin: 1px auto">
      <el-table-column prop="id" align="center" label="编号" width="180"> </el-table-column>
      <el-table-column prop="title" align="center" label="标题" width="180"> </el-table-column>
      <el-table-column prop="content" align="center" label="内容"> </el-table-column>
      <el-table-column prop="remark" align="center" label="备注"> </el-table-column>

      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="danger" @click="delPublish(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="添加公告" :visible.sync="dialogVisible" width="30%">
      <el-form :model="ruleForm" :rules="rules" ref="ruleFormRef" label-width="100px">
        <el-form-item label="公告标题" prop="title">
          <el-input placeholder="请输入标题" v-model="ruleForm.title"></el-input>
        </el-form-item>
        <el-form-item label="公告内容" prop="content">
          <el-input type="textarea" placeholder="请输入公告内容" :show-word-limit="true" v-model="ruleForm.content"></el-input>
        </el-form-item>
        <el-form-item label="公告备注" prop="remark">
          <el-input type="textarea" placeholder="请输入备注" v-model="ruleForm.remark"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'FirstProjectNotice',
  data() {
    return {
      search: '',
      dialogVisible: false,
      rules: {
        content: [
          { required: true, message: '请输入内容题', trigger: 'blur' },
          {
            min: 1,
            max: 500,
            message: '长度在 1 到 500 个字符',
            trigger: 'blur'
          }
        ],
        title: [{ required: true, message: '请输入标题', trigger: 'blur' }]
      },
      ruleForm: {
        title: '',
        content: '',
        remark: ''
      },
      tableData: []
    }
  },
  methods: {
    //删除数据
    async delPublish(id) {
      const confirmResult = await this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已经取消删除')
      }
      const { data: res } = await this.$http.post('/publish/delete', { id })
      if (res.code != 0) {
        return this.$message.error('删除失败')
      }
      this.$message.success('删除成功')
      this.getPublish()
    },
    ////获取数据
    async getPublish() {
      const { data: res } = await this.$http.get('/notice')
      if (res.code === 0) {
        this.tableData = res.data
      } else {
        return this.$message.error('获取列表数据失败')
      }
      console.log(res)
    },
    //添加数据
    async submitForm() {
      this.$refs.ruleFormRef.validate(async valid => {
        if (valid) {
          console.log(valid)
          let { title, content, remark } = this.ruleForm
          const { data: res } = await this.$http.post('/publish/add', { title, content, remark })
          if (res.code !== 0) {
            return this.$message.error('添加失败')
          }
          this.getPublish()
          this.$message.success('添加公告成功')
          this.dialogVisible = false
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  },
  created() {
    this.getPublish()
  }
}
</script>

<style lang="scss" scoped>
.el-table .warning-row {
  background: rgb(188, 241, 152);
}
</style>
